﻿using System;
using System.Collections.Generic;
using System.IO;
using TwitterBootstrapMVC;
using Web.UI.AdminLTE.Common;
using Web.UI.TypeExtensions;

namespace Web.UI.AdminLTE.Controls.Tab
{
    public class TabExtend : HtmlElementBase, IDisposable
    {
        private readonly TextWriter _textWriter;
        private Icon _headerIcon;
        private string _headerTitle;

        public TabExtend(TextWriter textWriter)
        {
            _textWriter = textWriter;
        }

        public override string ToHtmlString()
        {
            var tag = TagBuilderExtensions.New("li")
                .MergeHtmlAttributes(HtmlAttr.FormatHtmlAttributes());
            if (_headerIcon != null)
            {
                tag.AppendHtml(_headerIcon).AppendHtml(_headerTitle);
            }
            return tag.ToString();
        }

        public TabExtend Dropdown()
        {
            Class("dropdown");
            return this;
        }

        public TabExtend PullLeft()
        {
            Class("pull-left");
            return this;
        }

        public TabExtend PullRight()
        {
            Class("pull-right");
            return this;
        }

        public TabExtend Header(string icon, string title)
        {
            return Header(new Icon(icon), title);
        }

        public TabExtend Header(Icon icon, string title)
        {
            _headerIcon = icon;
            _headerTitle = title;
            Class("header");
            return this;
        }

        public TabExtend End()
        {
            _textWriter.Write(ToHtmlString().TrimEnd("</li>"));
            return this;
        }

        public void Dispose()
        {
            _textWriter.Write("</li>");
        }

        //  Common methods for all html elements
        #region Common Methods
        public TabExtend HtmlAttributes(IDictionary<string, object> htmlAttributes)
        {
            MergeHtmlAttributes(htmlAttributes);
            return this;
        }

        public TabExtend HtmlAttributes(object htmlAttributes)
        {
            MergeHtmlAttributes(htmlAttributes);
            return this;
        }

        public TabExtend Id(string id)
        {
            SetId(id);
            return this;
        }

        public TabExtend Class(string cssClass)
        {
            EnsureClass(cssClass);
            return this;
        }

        public TabExtend Data(object htmlDataAttributes)
        {
            MergeHtmlDataAttributes(htmlDataAttributes);
            return this;
        }

        #endregion
    }
}